package cn.ibdsr.human.controller;

import cn.ibdsr.human.constant.BizConstants;
import cn.ibdsr.human.constant.Const;
import cn.ibdsr.human.constant.GenderEnum;
import cn.ibdsr.human.constant.Result;
import cn.ibdsr.human.db.mapper.*;
import cn.ibdsr.human.db.model.*;
import cn.ibdsr.human.exception.BizExceptionEnum;
import cn.ibdsr.human.exception.BusinessException;
import cn.ibdsr.human.manager.LogFormatManager;
import cn.ibdsr.human.service.UserService;
import cn.ibdsr.human.transfer.PasswordDTO;
import cn.ibdsr.human.transfer.UserLoginDTO;
import cn.ibdsr.human.transfer.UserLoginResponse;

import cn.ibdsr.human.transfer.*;
import cn.ibdsr.human.util.RegUtils;
import com.alibaba.fastjson.JSONObject;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.conditions.query.QueryChainWrapper;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.validation.Valid;
import java.util.ArrayList;
import java.util.List;

/**
 * 对外暴露的用户相关的HTTP接口
 *
 * @author chenglx
 * @version V1.0
 * @createDate 2020-03-02 14:41
 * <p>
 * Date Author Description
 * ------------------------------------------------------
 * 2020-03-02 chenglx init
 */
@Api(tags = "登录|注册|修改手机号|修改密码 控制器")
@RestController
@RequestMapping(value = "/users")
@Slf4j
@Validated
public class UserController {
    @Resource
    private UserService userService;

    @Resource
    private LogFormatManager logFormatManager;
    /**
     * cdl-ok
     * 登录
     *
     * @param userLoginDTO
     */
    @ApiOperation("平台端|企业|用户——登录")
    @PostMapping("/login")
    @ResponseBody
    public Result<UserLoginResponse> login(@RequestBody @Valid UserLoginDTO userLoginDTO) {
        logFormatManager.formatKeyPointLog("平台端|企业|用户——登录", JSONObject.toJSONString(userLoginDTO), BizConstants.MODULE_USER);
        UserLoginResponse ticketSeller = userService.loginOperate(userLoginDTO);
        return new Result<>(ticketSeller);
    }

    /**
     * cdl-ok
     * 修改密码
     *
     * @param passwordDTO
     */
    @ApiOperation("企业|用户——修改密码")
    @PutMapping("/passwords")
    @ResponseBody
    public Result<Boolean> changePwd(@RequestAttribute(name = Const.USER_ID ) Long userId, @RequestBody @Valid PasswordDTO passwordDTO) {
        logFormatManager.formatKeyPointLog("企业|用户——修改密码","userId="+userId+":"+JSONObject.toJSONString(passwordDTO), BizConstants.MODULE_USER);
        userService.changePwd(passwordDTO, userId);
        return new Result<>(true);
    }
    /**
     * cdl-ok
     * 修改手机号
     *
     * @param modifyPhoneDTO
     */
    @ApiOperation("企业-修改手机号")
    @PutMapping("/new-phone")
    @ResponseBody
    public Result<Boolean> changPhone(@RequestAttribute(name = Const.USER_ID ) Long userId, @RequestBody @Valid ModifyPhoneDTO modifyPhoneDTO) {
        logFormatManager.formatKeyPointLog("企业-修改手机号","userId="+userId+":"+JSONObject.toJSONString(modifyPhoneDTO), BizConstants.MODULE_USER);
        userService.changPhone(modifyPhoneDTO, userId);
        return new Result<>(true);
    }
    /**
     * cdl-ok
     * 注册
     *
     * @param registerDTO
     */
    @ApiOperation("企业|用户——注册")
    @PostMapping("/new-user")
    @ResponseBody
    public Result<UserLoginResponse> register(@RequestBody @Valid  RegisterDTO registerDTO) {
        logFormatManager.formatKeyPointLog("企业|用户——注册",JSONObject.toJSONString(registerDTO), BizConstants.MODULE_USER);
        if(!registerDTO.getPhone().matches(RegUtils.MOBILE_PHONE)){
            throw new BusinessException(BizExceptionEnum.PHONE_IS_ERROR);
        }
        UserLoginResponse userLoginResponse = userService.register(registerDTO);
        return new Result<>(userLoginResponse);
    }


}
